/**
 * @param {number[]} nums
 * @param {number} target
 * @return {number[]}
 */
export var searchRange = function (nums, target) {
  return [search(nums, target), search(nums, target, true)]
}

function search(nums, target, findright = false) {
  let left = 0,
    right = nums.length - 1
  let ans = -1
  while (left <= right) {
    const mid = (left + right) >> 1
    if (nums[mid] === target) {
      ans = mid
      if (findright) {
        left = mid + 1
      } else {
        right = mid - 1
      }
    } else if (nums[mid] > target) {
      right = mid - 1
    } else {
      left = mid + 1
    }
  }
  return ans
}
